太久沒用紀錄一下
感謝保哥知識分享
舉例我們的為網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath
=> /
Request.PhysicalPath
=> D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath)
=> D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath
=> D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath)
=> Content.aspx
Request.CurrentExecutionFilePath
=> /News/Press/Content.aspx
Request.FilePath
=> News/Press/Content.aspx
Request.Path
=> /News/Press/Content.aspx/123
Request.RawUrl
=> /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath
=> /News/Press/Content.aspx/123
Request.Url.AbsoluteUri
=> http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme
=> http
Request.Url.Host
=> localhost
Request.Url.Port
=> 1897
Request.Url.Authority
=> localhost:1897
Request.Url.LocalPath
=> /News/Press/Content.aspx/123
Request.PathInfo
=> /123
Request.Url.PathAndQuery
=> /News/Press/Content.aspx/123?id=1
Request.Url.Query
=> ?id=1
Request.Url.Fragment
=>
Request.Url.Segments
=> /
=> News/
=> Press/
=> Content.aspx/
=> 123
所以當你看了這張表之後,你還會想用 Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/", 1)-1) 這種寫法嗎?
用這樣寫 Request.Url.Segments[1].Replace("/", "") 不是又短又直覺嗎? ^_^
注意事項
當程式部署(Deploy)到 IIS 6 之後,假設你的網址是 http://localhost:1897/News/Press/Content.aspx/123? ,IIS 不知為何會將問號 ( ? ) 給刪除掉,以致於 ASP.NET 使用 Request.Url.Query 是空字串!如果你的程式有判斷到這部分時就要特別注意,因為照理說應該會抓到才對!不知道是不是 IIS 6 的 Bug
資料來源 來自 The Will Will Web
=>
https://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object